---@class WenheYanbing.RecruitMinionHandler : RequestHandler
local RecruitMinionHandler = class("WenheYanbing.RecruitMinionHandler")

function RecruitMinionHandler:initialize(player, data)
    self.player = player
    self.data = data
    self.scene = self:createScene()
end

function RecruitMinionHandler:createScene()
    local scene = {}
    scene.tavernMinions = self.player.tavernMinions
    scene.gold = self.player.gold
    scene.hand = self.player.hand
    scene.board = self.player.board
    scene.tavernTier = self.player.tavernTier
    scene.upgradeCost = self.player:getUpgradeCost()
    scene.freezeTavern = self.player.freezeTavern
    return scene
end

function RecruitMinionHandler:setup()
    self.player:notifyUI("WenheYanbing.RecruitMinion", self.scene)
end

function RecruitMinionHandler:update(data)
    if data.action == "buy" then
        return self:handleBuy(data.tavernIndex)
    elseif data.action == "refresh" then
        return self:handleRefresh()
    elseif data.action == "levelup" then
        return self:handleLevelUp()
    elseif data.action == "freeze" then
        return self:handleFreeze()
    elseif data.action == "place" then
        return self:handlePlace(data.handIndex, data.boardPosition)
    elseif data.action == "sell" then
        return self:handleSell(data.boardPosition)
    end
    return false
end

function RecruitMinionHandler:handleBuy(tavernIndex)
    if self.player:buyMinion(tavernIndex) then
        self.scene = self:createScene()
        return false  -- 不结束交互，继续招募
    end
    return false
end

function RecruitMinionHandler:handleRefresh()
    if self.player:refreshTavern() then
        self.scene = self:createScene()
        return false
    end
    return false
end

function RecruitMinionHandler:handleLevelUp()
    if self.player:upgradeTavern() then
        self.scene = self:createScene()
        return false
    end
    return false
end

function RecruitMinionHandler:handleFreeze()
    if self.player:freezeTavern() then
        self.scene = self:createScene()
        return false
    end
    return false
end

function RecruitMinionHandler:handlePlace(handIndex, boardPosition)
    if self.player:placeMinion(handIndex, boardPosition) then
        self.scene = self:createScene()
        return false
    end
    return false
end

function RecruitMinionHandler:handleSell(boardPosition)
    if self.player:sellMinion(boardPosition) then
        self.scene = self:createScene()
        return false
    end
    return false
end

function RecruitMinionHandler:finish()
    -- 结束招募阶段
end

return RecruitMinionHandler